package com.ergwang.controller.admin;

import com.ergwang.Model.Result;
import com.ergwang.Model.admin.bo.ChangeOrderBO;
import com.ergwang.Model.admin.bo.OrdersByPageBO;
import com.ergwang.Model.admin.vo.OrderGetInfoVO;
import com.ergwang.Model.admin.vo.PageOrdersVO;
import com.ergwang.service.admin.OrderService;
import com.ergwang.service.admin.OrderServiceImpl;
import com.ergwang.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author：ergwang
 * @Date: 2020/5/31
 * @Time: 10:54
 */
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImpl();
    private Gson gson= new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("ordersByPage".equals(action)){
            ordersByPage(request,response);
        }else if ("changeOrder".equals(action)){
            changeOrder(request,response);
        }
    }

    /**
     * 修改订单，（暂时只实现修改订单状态）
     * @param request
     * @param response
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangeOrderBO changeOrderBO = gson.fromJson(requestBody, ChangeOrderBO.class);
        orderService.changeOrder(changeOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     *
     * 根据请求体中的需求,分页查询显示
     * 从数据库获取订单列表，返回给前端
     * @param request
     * @param response
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrdersByPageBO ordersByPageBO = gson.fromJson(requestBody, OrdersByPageBO.class);
        PageOrdersVO orders = orderService.ordersByPage(ordersByPageBO);
        response.getWriter().println(gson.toJson(Result.ok(orders)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        } else if ("order".equals(action)){
            OrderGetInfo(request,response);
        }
    }

    /**
     * 根据请求传来的id 获取订单的信息，用于修改订单
     * @param request
     * @param response
     */
    private void OrderGetInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        OrderGetInfoVO order = orderService.OrderGetInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(order)));
    }

    /**
     * 根据前端传过来的id 删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        orderService.deleteOrder(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }
}
